package com.mall.server.controller.yxcontroller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.commons.R;
import com.mall.commons.exceotion.BusinessException;
import com.mall.commons.exceotion.ErrorResult;
import com.mall.server.service.CouponService;
import com.tanhua.model.domain.SmsCoupon;
import com.tanhua.model.vo.CouponIdVo;
import com.tanhua.model.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@RequestMapping("/coupon")
public class CouponController {
    @Autowired
    private CouponService couponService;

    /**
     * 根据优惠券名称和类型分页获取优惠券列表
     * @param name
     * @param pageNum
     * @param pageSize
     * @param type
     * @return
     */
    @GetMapping("/list")
    public R couponList(String name, @RequestParam(defaultValue = "1")Integer pageNum,
                        @RequestParam(defaultValue = "5")Integer pageSize,Integer type){
        Page<SmsCoupon> page = new Page<>(pageNum,pageSize);
        PageVo pageVo =couponService.couponList(name,type,page);
        return R.success(pageVo);
    }

    /**
     *根据id获取单个优惠券的详细信息
     * 三表 SmsCoupon SmsCouponProductCategoryRelation SmsCouponProductRelation
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R findOneById(@PathVariable("id") Long id){
        CouponIdVo couponIdVo =couponService.findOneById(id);
        return R.success(couponIdVo);
    }


    /**
     * 添加优惠券 sms_coupon sms_coupon_product_category_relation  sms_coupon_product_relation
     * @param couponIdVo
     * @return
     */
    @PostMapping("/create")
    public R create(@RequestBody CouponIdVo couponIdVo){
        if (couponIdVo ==null){
            throw new BusinessException(ErrorResult.parameterError());
        }
        couponService.create(couponIdVo);
        return R.success("");
    }
    /**
     * 修改优惠券
     * @param couponIdVo
     * @return
     */
    @PostMapping("/update/{id}")
    public R update(@RequestBody CouponIdVo couponIdVo,@PathVariable("id") Long id){
        if (couponIdVo ==null){
            throw new BusinessException(ErrorResult.parameterError());
        }
        couponService.update(couponIdVo);
        return R.success("");
    }

    /**
     * 删除优惠卷
     */
    @PostMapping("/delete/{id}")
    public R delete(@PathVariable("id") Long id){
        couponService.delete(id);
        return R.success("");
    }

}
